package com.videoweb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.videoweb.entity.Favorites;
import com.videoweb.entity.VideoFavorite;
import com.videoweb.entity.dto.CheckFavoritesDTO;
import com.videoweb.entity.vo.FavoritesVo;
import com.videoweb.mapper.FavoritesMapper;
import com.videoweb.mapper.VideoFavoriteMapper;
import com.videoweb.service.IFavoritesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.videoweb.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lmr
 * @since 2025-05-17
 */
@Service
public class FavoritesServiceImpl extends ServiceImpl<FavoritesMapper, Favorites> implements IFavoritesService {

    @Autowired
    FavoritesMapper favoritesMapper;

    @Autowired
    VideoFavoriteMapper videoFavoriteMapper;



//收藏夹表部分  添加  del
    @Override
    public boolean createFavorites(Favorites favorites) {
        int row =favoritesMapper.insert(favorites);
        return row >0;
    }

    @Override
    public boolean removeFavoritesByFavorId(Long favoriteId) {

        int row = favoritesMapper.deleteById(favoriteId);
        return row >0;
    }

    @Override
    public List<Favorites> getAllFavoritesByUserId(Long userId) {

        LambdaQueryWrapper<Favorites> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Favorites::getUserId,userId);
        if (SecurityUtils.getUserId()!= userId) {
//            得到的是公开的而不是隐私的
            queryWrapper.eq(Favorites::getIsPublic,1);
        }

        return this.list(queryWrapper);
    }



    //收藏夹中间表 表部分  添加  del

    @Override
    public boolean addVideoFavorites(VideoFavorite videoFavorite) {
        videoFavorite.setUserId(SecurityUtils.getUserId());
        int row =videoFavoriteMapper.insert(videoFavorite);
        return row >0;
    }

    @Override
    public boolean cancelVideoFromFavorites(Long videoId, Long favoritesId ) {
        LambdaQueryWrapper<VideoFavorite> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(VideoFavorite::getFavoritesId ,favoritesId)
                    .eq(VideoFavorite::getVideoId,videoId);

        int row = this.videoFavoriteMapper.delete(queryWrapper);

        return row >0;

    }


//          检查视频是否被用户收藏了
@Override
public CheckFavoritesDTO checkVideoFavorites(Long videoId) {


    // 1. 查询用户收藏夹中包含该视频的所有收藏夹
    Long userId = SecurityUtils.getUserId();
    List<FavoritesVo> favoritesList = favoritesMapper.checkVideoFavorites(videoId, userId);





    // 2. 构建 DTO
    CheckFavoritesDTO dto = new CheckFavoritesDTO();

    if (favoritesList != null && !favoritesList.isEmpty()) {
        // 已收藏
        dto.setFavorited(true);

        // 提取 favoritesIds
        List<Long> favoritesIds = favoritesList.stream()
                .map(FavoritesVo::getId)
                .toList();

        dto.setFavoritesIds(favoritesIds);
        dto.setFavoritesList(favoritesList);
    } else {
        // 未收藏
        dto.setFavorited(false);
        dto.setFavoritesIds(Collections.emptyList());
        dto.setFavoritesList(Collections.emptyList());
    }

    return dto;
}


@Transactional
    public boolean saveBatchFavorites(Long videoId, List<Long> favoritesIds) {
//        Long userId = 2L;
        Long userId = SecurityUtils.getUserId();
        List<VideoFavorite> list = new ArrayList<>();
        for (Long favoritesId : favoritesIds) {
            VideoFavorite vf = new VideoFavorite();
            vf.setUserId(userId);
            vf.setVideoId(videoId);
            vf.setFavoritesId(favoritesId);
            list.add(vf);
        }
//   TODO 要优化一下判断的逻辑
           videoFavoriteMapper.insert(list);
        return true;
    }




}
